package com.kmproject.haifu.mvp.presenter;

import android.app.Application;

import com.jess.arms.http.imageloader.glide.ImageConfigImpl;
import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.utils.ArmsUtils;
import com.kmproject.haifu.app.utils.RxUtils;
import com.kmproject.haifu.mvp.contract.AddUserContract;
import com.kmproject.haifu.mvp.model.entity.ResultBean;
import com.kmproject.haifu.mvp.model.entity.User;
import com.kmproject.haifu.mvp.ui.widget.CircleImageView;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.functions.Consumer;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;

import javax.inject.Inject;


//import okhttp3.MediaType;
//import okhttp3.MultipartBody;
//import okhttp3.RequestBody;


@ActivityScope
public class AddUserPresenter extends BasePresenter<AddUserContract.Model, AddUserContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;
    ImageLoader glideImage;
    @Inject
    public AddUserPresenter(AddUserContract.Model model, AddUserContract.View rootView) {
        super(model, rootView);
    }

    /**
     * 添加修改用户
     * @param user
     */
   public void addUser(User user)
   {
       mModel.addUser(ArmsUtils.obtainAppComponentFromContext(mAppManager.getCurrentActivity()).gson().toJson(user).toString()).compose(RxUtils.applySchedulers(mRootView))
               .subscribe(new Consumer<ResultBean<User>>() {
                   @Override
                   public void accept(ResultBean<User> resultBean) throws Exception {
                         mRootView.addSuccess(resultBean.getData());

                   }
               }, new Consumer<Throwable>() {
                   @Override
                   public void accept(Throwable throwable) throws Exception {
                       mRootView.showMessage(throwable.getMessage());
                   }
               });
   }
    /**
     * 上传图片
     * @param path
     */
    public void updateHeadImg(final CircleImageView circleImageView, String path)
    {
//        File file=new File(path);
//        MultipartBody.Builder builder = new MultipartBody.Builder()
//                .setType(MultipartBody.FORM);//表单类型
////        RequestBody imageBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
////        builder.addFormDataPart("multipartFie", file.getName(), imageBody);//imgfile 后台接收图片流的参数名
////        MultipartBody.Part body = MultipartBody.Part.createFormData("multipartFie", file.getName(), imageBody);
//        RequestBody requestFile =
//                RequestBody.create(MediaType.parse("multipart/form-data"), file);
//        MultipartBody.Part body =
//                MultipartBody.Part.createFormData("multipartFie", file.getName(), requestFile);
//        mModel.updateHeader(body).compose(RxUtils.applySchedulers(mRootView))
//                .subscribe(new Consumer<String>() {
//                    @Override
//                    public void accept(String resultBean) throws Exception {
//                    mRootView.updateHeadSuccess(resultBean);
//                        if (glideImage == null) {
//                            glideImage = ArmsUtils.obtainAppComponentFromContext(mAppManager.getCurrentActivity()).imageLoader();
//                        }
//                        glideImage.loadImage(mAppManager.getCurrentActivity(), ImageConfigImpl.builder().url(resultBean).isCrossFade(true).isCenterCrop(true)
//                                .imageView(circleImageView)
//                                .build());
//                    }
//                }, new Consumer<Throwable>() {
//                    @Override
//                    public void accept(Throwable throwable) throws Exception {
//
//                    }
//                });
    }

    /**
     * 初始化年龄
     * @return
     */
    public List<String> getAges(){
        List<String> ages=new ArrayList<>();
        for (int i=1;i<101;i++)
        {
            ages.add(i+"");
        }
        return ages;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }
}
